<?php
namespace User\Controller;
use Common\Controller\BehaviorController;
use Common\Model\UserModel;

class IndexController extends BehaviorController {



    public function register(){
        $this->display("/user/register");
    }

    public function login(){
        if( $this->IsLogin(false) ){
            $this->redirect(U("user/manager/index"));
        }
        $this->display("/user/login");
    }

    public function getPassWord(){
        if(IS_POST){
            $to = I('email');
            $user = I('userName');
            $model = new UserModel();
            $userData = $model->getUserByName($user);
            if($userData && $userData['email'] && $userData['email'] == $to){
                $code = rand(50000,59999);
                $webName = C('cfg_webname');
                $webUrl = C('cfg_basehost');
                C("URL_MODEL",0);
                $md5Codel = md5($code);
                $url = U('user/index/resetpassword',array('p'=>$md5Codel,'user'=> $user),'',true);

                $title = "找回密码邮件";
                $key = 'getPassWord_'. $user . $md5Codel ;
                $dta = array(
                    'user' => $user,
                    'code' => $code,
                    'id'   => $userData['id']
                );
                F($key,$dta);

                $contentStr = C('cfg_mail_getpassword');

                $content = <<<EOF
<div style="font:14px/1.5 'Lucida Grande', '微软雅黑';color:#333;"><p style="font:14px/1.5 'Lucida Grande';margin:0;"><br></p><div class="mail_quote_639B3071730B44F0B10836C949133DF4" style="font: 14px/1.5 'Lucida Grande';color:#333;">
<style></style>
<div style="background-color:#d0d0d0;background-image:url(http://weixin.qq.com/zh_CN/htmledition/images/weixin/letter/mmsgletter_2_bg.png);text-align:center;padding:40px;">
	<div class="mmsgLetter" style="width:580px;margin:0 auto;padding:10px;color:#333;background-color:#fff;border:0px solid #aaa;border-radius:5px;-webkit-box-shadow:3px 3px 10px #999;-moz-box-shadow:3px 3px 10px #999;box-shadow:3px 3px 10px #999;font-family:Verdana, sans-serif; ">
		<div class="mmsgLetterHeader" style="height:23px;background:url(http://weixin.qq.com/zh_CN/htmledition/images/weixin/letter/mmsgletter_2_bg_topline.png) repeat-x 0 0;">
		</div>
		<div class="mmsgLetterContent" style="text-align:left;padding:30px;font-size:14px;line-height:1.5;">
			<div>
				<p class="salutation" style="font-weight:bold;">
					亲爱的{$webName}用户<span id="mailUserName">{$user}</span>，您好！：
				</p>
				 <p>忘记密码了吗？别着急，请点击以下链接，我们协助您找回密码：<br>
<a href="{$url}" style="word-break:break-all;word-wrap:break-word;display:block">{$url}</a><br>
<br>如果这不是您的邮件请忽略，很抱歉打扰您，请原谅。</p>
提示：重置密码后本链接将失效。<br />
本邮件由系统自动发送，请勿直接回复！<br />
感谢您的访问，祝您使用愉快<br />
<a href="{$webUrl}">{$webName}</a>
			</div>
			<div class="mmsgLetterInscribe" style="padding:40px 0 0;">

				<div class="mmsgSender" style="margin:0 0 0 54px;">
					<p class="mmsgName" style="margin:0 0 10px;">Admin</p>
					<p class="mmsgInfo" style="font-size:12px;margin:0;line-height:1.2;">十堰信息网<br>shiyanweb@163.com</p>
				</div>
			</div>
		</div>
		<div class="mmsgLetterFooter" style="margin:16px;text-align:center;font-size:12px;color:#888;text-shadow:1px 0px 0px #eee;">
		</div>
	</div>
</div>
</div></div>
EOF;
                $res = send_email($to,$title,$content);

                if($res['error'] == 0){
                    $this->display("/user/getpassword_success");
                }else{
                    $this->error('邮件发送失败,请稍后再次尝试');
                }
            }else{
                $this->error('用户名,邮箱不匹配');
            }


        }
        $this->display("/user/user_getpassword");
    }

    public function resetpassword(){

        $user = I('get.user');
        $code = I('get.p');
        $key = 'getPassWord_'. $user . $code ;
        $res = F($key);

        if($res || md5($res['code']) == $code){
            if(IS_POST){
                $model = new UserModel();
                if(I('post.pass') == I('post.pass2')){
                    $id = $res['id'];
                    $user = $model->find($id);
                    if( $user['password'] == md5(I('post.pass') .  C('PASSWORD_KEY'))){
                        $this->error('设置密码不能与旧密码一样');
                    }
                    $data['password'] = md5(I('post.pass') . C('PASSWORD_KEY'));
                    $data['id'] = $res['id'];
                    $res = $model->save($data);
                    if($res){
                        F($key,null);
                        $this->success('密码修改成功',UU('User/index/login'));
                    }else{
                        $this->error('密码修改失败,请重试');
                    }
                }else{
                    $this->error('两次密码不一致');
                }
            }
            $this->display("/user/editpassword");
        }else{
            $this->display("/user/editpassword_error");
        }
    }


    public function quit(){
        $key = sprintf('%s_uid',C('COOK_LOGIN_KEY'));
        cookie($key,null);
        $this->redirect("Info/index/index");
    }

    public function checkUserName(){
        $name = I('name');
        if($name){
            $userModel = new UserModel();
            $username = $userModel->getUserByName($name);
            if($username){
                echo '<div class=\'error_text\'>用户名已注册</div>';
            }else{
                echo '<div class=\'right_text\'>用户名可以注册</div>';
            }
        }
    }

    public function loginCheck(){
        if (IS_POST) {
            $userModel = new UserModel();
            if (!$userModel->LoginCheck()) {
                $this->error($userModel->getError());
                exit();
            }else{
                $this->redirect("User/manager/index");
            }

        }
    }

    public function reg(){
        if (IS_POST) {
            $userName = I("UserName");
            $userModel = new UserModel();
            if (!$userModel->addUser()) {
                $this->error($userModel->getError());
                exit();
            }else{
                $this->redirect("User/index/regSuccess",array('username'=>$userName));
            }

        }
    }

    public function regSuccess(){
        $userName = I("username");
        $this->assign('userName',$userName);
        $this->display('/user/reg_success');
    }

    public function checkCode($code){
        $verify = new \Org\Util\Verify();
        $bool = $verify->check($code);
        if($bool){
            return true;
        }else{
            return false;
        }
    }

    public function validateCode()
    {
        //实例化验证码
        $Verify = new \Think\Verify();
        $Verify->fontSize = 16;//字体大小
        $Verify->length = 4;//长度
        $Verify->useNoise = true;//关闭杂点
        $Verify->useCurve = false;
        $Verify->useImgBg = false;//是否使用背景图片
        $Verify->imageW = 108;//长
        $Verify->imageH = 30;//宽
        $Verify->bg = array(236, 236, 236);//背景颜色
        $Verify->entry();
    }

    public function favorite(){
        $favorite = cookie('favorite');
        if($favorite){
            $fidArr = array_filter(explode(',',$favorite));
            $info =  $this->getinfoInInfoId($fidArr);
            $this->assign('info',$info);
        }
        $this->display('/user/user_favorite');
    }

}